package com.yaolonga.itfc.manager.core.utils;


import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class SystemUtil {

    public static String getOsName() {
        String osName = System.getProperty("os.name");
        if (osName == null) {
            return "";
        }
        return osName;
    }

    public static String getOsArch() {
        String osArch = System.getProperty("os.arch");
        if (osArch == null) {
            return "";
        }
        return osArch;
    }

    public static String getOsVersion() {
        String osVersion = System.getProperty("os.version");
        if (osVersion == null) {
            return "";
        }
        return osVersion;
    }

    /**
     * 判断操作系统是否是 Windows
     *
     * @return true：操作系统是 Windows
     * false：其它操作系统
     */
    public static boolean isWindows() {
        String osName = getOsName();

        return osName.toLowerCase().startsWith("windows");
    }

    /**
     * 判断操作系统是否是 MacOS
     *
     * @return true：操作系统是 MacOS
     * false：其它操作系统
     */
    public static boolean isMacOs() {
        String osName = getOsName();

        return osName.toLowerCase().startsWith("mac");
    }

    /**
     * 判断操作系统是否是 Linux
     *
     * @return true：操作系统是 Linux
     * false：其它操作系统
     */
    public static boolean isLinux() {
        String osName = getOsName();

        return (osName.toLowerCase().startsWith("linux")) || (!isWindows() && !isMacOs());
    }

    public static String getLineSeprator() {
        String lineSeprator = System.getProperty("line.separator");
        if (lineSeprator == null) {
            return "";
        }
        return lineSeprator;
    }

    public static String getUsername() {
        String username = System.getProperty("user.name");
        if (username == null) {
            return "";
        }
        return username;
    }

    public static String getUserHome() {
        String userhome = System.getProperty("user.home");
        if (userhome == null) {
            return "";
        }
        return userhome;
    }

    public static String getUserDir() {
        String userdir = System.getProperty("user.dir");
        if (userdir == null) {
            return "";
        }
        return userdir;
    }


    public static Map<String, String> getLinuxOpSystemInfo() {
        HashMap<String, String> info = new HashMap<>();
        if (isLinux()) {
            Process process = CommandUtils.execCmd("cat /etc/os-release");
            InputStream inputStream = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {

                    line = line.trim();
                    if (line.isEmpty()){
                        continue;
                    }
                    String[] split = line.split("=");
                    if (split.length > 1){
                        String key = split[0].trim();
                        String value = split[1].trim();
                        if ((value.startsWith("\"") && value.endsWith("\"")) || (value.startsWith("'") && value.endsWith("'"))){
                            value = value.substring(1,value.length()-1);
                        }
                        info.put(key,value);
                    }
                }
            } catch (Exception ignore) {
            }

        }
        return info;
    }

    public static void main(String[] args) {
        System.out.println(getLinuxOpSystemInfo());
    }
}
